﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class ReceptionEdit : System.Web.UI.Page
    {
        protected ReqReception reqReception;
        protected string empName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string formNo = Request.QueryString["formNo"];
                if (formNo != null)
                {
                    reqReception = ctx.ReqReceptions.Single(r => r.FormNo == formNo);
                    Employee employee = ctx.Employees.Single(r => r.Id == reqReception.EmpId);
                    empName = employee.Name;
                    Customer.Value = reqReception.Customer;
                    CustomerCnt.Value = reqReception.CustomerCnt;
                    CustomerAddr.Value = reqReception.CustomerAddr;
                    CustomerAccompanies.Value = reqReception.CustomerAccompanies;
                    CorpAccompanies.Value = reqReception.CorpAccompanies;
                    PeopleCnt.Value = reqReception.PeopleCnt;
                    PreCost.Value = reqReception.PreCost;
                    MealStd.Value = reqReception.MealStd;
                    MealCnt.Value = reqReception.MealCnt;
                    RestaurantStd.Value = reqReception.RestaurantStd;
                    RestaurantCnt.Value = reqReception.RestaurantCnt;
                    VehicleSchedule.Value = reqReception.VehicleSchedule;
                    Cause.Value = reqReception.Cause;
                }
                else
                {
                    EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                    empName = ebi.Name;
                    reqReception = new ReqReception();
                    reqReception.ReqDate = DateTime.Now;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            _formSave(0);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            _formSave(1);
        }
        private void _formSave(int isSubmit)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string formNo = Request.QueryString["formNo"];
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if (!string.IsNullOrEmpty(formNo) && formNo != "undefined")
            {
                reqReception = ctx.ReqReceptions.Single(r => r.FormNo == formNo);
                //删除之前的签核数据
                var list = from r in ctx.SignFlows where r.FormNo == formNo select r;
                ctx.SignFlows.DeleteAllOnSubmit(list);
            }
            else
            {
                reqReception = new ReqReception();
                string maxFormNo = ctx.ReqReceptions.Max(r => r.FormNo);
                formNo = "Form_Rece_000001";
                if (!string.IsNullOrEmpty(maxFormNo))
                {
                    formNo = "Form_Rece_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
                }
                reqReception.FormNo = formNo;
                reqReception.ReqDate = DateTime.Now;
                reqReception.EmpId = ebi.Id;
                ctx.ReqReceptions.InsertOnSubmit(reqReception);
            }
            reqReception.Customer = Customer.Value;
            reqReception.CustomerCnt = CustomerCnt.Value;
            reqReception.CustomerAddr = CustomerAddr.Value;
            reqReception.CustomerAccompanies = CustomerAccompanies.Value;
            reqReception.CorpAccompanies = CorpAccompanies.Value;
            reqReception.PeopleCnt = PeopleCnt.Value;
            reqReception.PreCost = PreCost.Value;
            reqReception.MealStd = MealStd.Value;
            reqReception.MealCnt = MealCnt.Value;
            reqReception.RestaurantStd = RestaurantStd.Value;
            reqReception.RestaurantCnt = RestaurantCnt.Value;
            reqReception.VehicleSchedule = VehicleSchedule.Value;
            reqReception.Cause = Cause.Value;
            reqReception.StatusId = isSubmit;
            ctx.SubmitChanges();
            if (isSubmit == 1)//写签核流程
            {
                FlowSet fs = new FlowSet();
                fs.SetSignFlow(reqReception.FormNo, ebi);
            }
            Response.Write("<script>alert('修改成功');window.returnValue='" + reqReception.FormNo + "';window.close();</script>");
        }
    }
}